package com.ruoyi.eduManage.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.eduManage.domain.EduClass;
import com.ruoyi.eduManage.domain.EduTeacher;
import com.ruoyi.eduManage.mapper.EduClassMapper;
import com.ruoyi.eduManage.service.IEduClassService;
import com.ruoyi.eduManage.service.IEduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 班级信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-24
 */
@Service
public class EduClassServiceImpl implements IEduClassService
{
    @Autowired
    private EduClassMapper eduClassMapper;
    
    @Autowired
    private IEduTeacherService eduTeacherService;

    /**
     * 查询班级信息
     * 
     * @param classId 班级信息主键
     * @return 班级信息
     */
    @Override
    public EduClass selectEduClassByClassId(Long classId)
    {
        return eduClassMapper.selectEduClassByClassId(classId);
    }

    /**
     * 查询班级信息列表
     * 
     * @param eduClass 班级信息
     * @return 班级信息
     */
    @Override
    public List<EduClass> selectEduClassList(EduClass eduClass)
    {
        return eduClassMapper.selectEduClassList(eduClass);
    }

    /**
     * 新增班级信息
     * 
     * @param eduClass 班级信息
     * @return 结果
     */
    @Override
    public int insertEduClass(EduClass eduClass)
    {
        eduClass.setCreateTime(DateUtils.getNowDate());
        return eduClassMapper.insertEduClass(eduClass);
    }

    /**
     * 修改班级信息
     * 
     * @param eduClass 班级信息
     * @return 结果
     */
    @Override
    public int updateEduClass(EduClass eduClass)
    {
        eduClass.setUpdateTime(DateUtils.getNowDate());
        return eduClassMapper.updateEduClass(eduClass);
    }

    /**
     * 批量删除班级信息（逻辑删除）
     * 
     * @param classIds 需要删除的班级信息主键
     * @return 结果
     */
    @Override
    public int deleteEduClassByClassIds(Long[] classIds)
    {
        int rows = 0;
        for (Long classId : classIds)
        {
            rows += deleteEduClassByClassId(classId);
        }
        return rows;
    }

    /**
     * 删除班级信息信息
     * 
     * @param classId 班级信息主键
     * @return 结果
     */
    @Override
    public int deleteEduClassByClassId(Long classId)
    {
        EduClass eduClass = new EduClass();
        eduClass.setClassId(classId);
        eduClass.setDelFlag("1");
        eduClass.setUpdateTime(DateUtils.getNowDate());
        return eduClassMapper.updateEduClass(eduClass);
    }
    
    /**
     * 根据班级编码更新学生人数
     * 
     * @param classCode 班级编码
     * @param increment 增加的学生数量（正数为增加，负数为减少）
     * @return 结果
     */
    @Override
    public int updateStudentCount(String classCode, int increment)
    {
        // 根据班级编码查询班级信息
        EduClass eduClass = new EduClass();
        eduClass.setClassCode(classCode);
        List<EduClass> classList = eduClassMapper.selectEduClassList(eduClass);
        
        if (classList == null || classList.isEmpty()) {
            return 0;
        }
        
        // 获取班级信息并更新学生人数
        EduClass updateClass = classList.get(0);
        Long currentCount = updateClass.getStudentCount();
        
        // 如果当前学生人数为空，则设置为0
        if (currentCount == null) {
            currentCount = 0L;
        }
        
        // 计算新的学生人数
        Long newCount = currentCount + increment;
        if (newCount < 0) {
            newCount = 0L; // 确保学生人数不为负数
        }
        
        // 更新班级学生人数
        updateClass.setStudentCount(newCount);
        updateClass.setUpdateTime(DateUtils.getNowDate());
        
        return eduClassMapper.updateEduClass(updateClass);
    }
    
    /**
     * 根据教师ID获取教师信息
     * 
     * @param teacherId 教师ID
     * @return 教师信息
     */
    @Override
    public EduTeacher getTeacherNameById(Long teacherId)
    {
        return eduTeacherService.selectEduTeacherByTeacherId(teacherId);
    }
}
